package data;

import entity.Book;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @title: BookDao
 * @projectName BMS
 * @description: BookDao
 * @date 2024/12/21 17:13
 */
public class BookDao {
    /**
     * 通过图书编号 获取一个图书对象
     * @param id
     * @return
     */
    public static Book getBookById(String id){
        Book book = null;
        if(id != null){
            String sql = "select * from book where id = '"+id+"'";
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    book = new Book();
                    book.setId(rs.getString("id"));
                    book.setName(rs.getString("name"));
                    book.setType(rs.getString("type"));
                    book.setAuthor(rs.getString("author"));
                    book.setTranslator(rs.getString("translator"));
                    book.setPublisher(rs.getString("publisher"));
                    book.setPublish_time(rs.getString("publish_time"));
                    book.setStock(rs.getInt("stock"));
                    book.setPrice(rs.getDouble("price"));
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return book;
    }

    /**
     * 根据图书名 返回图书对象
     * @param name
     * @return
     */
    public static Book getBookByName(String name){
        Book book = null;
        if(name != null){
            String sql = "select * from book where name = '"+name+"'";
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    book = new Book();
                    book.setId(rs.getString("id"));
                    book.setName(rs.getString("name"));
                    book.setType(rs.getString("type"));
                    book.setAuthor(rs.getString("author"));
                    book.setTranslator(rs.getString("translator"));
                    book.setPublisher(rs.getString("publisher"));
                    book.setPublish_time(rs.getString("publish_time"));
                    book.setStock(rs.getInt("stock"));
                    book.setPrice(rs.getDouble("price"));

                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return book;
    }

    /**
     * 通过sql查询语句 获取图书对象列表
     * @param sql
     * @return
     */
    public static List<Book> selectBookList(String sql){
        List<Book> bookList = new ArrayList<Book>();
        if(sql != null){
            Book book = new Book();
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                while(rs.next()){
                    book = new Book();
                    book.setId(rs.getString("id"));
                    book.setName(rs.getString("name"));
                    book.setType(rs.getString("type"));
                    book.setAuthor(rs.getString("author"));
                    book.setTranslator(rs.getString("translator"));
                    book.setPublisher(rs.getString("publisher"));
                    book.setPublish_time(rs.getString("publish_time"));
                    book.setStock(rs.getInt("stock"));
                    book.setPrice(rs.getDouble("price"));
                    bookList.add(book);
                }
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return bookList;
    }

    /**
     * 获取所有对象图书列表
     * @return
     */
    public static List<Book> selectBookList(){
        String sql = "select * from book";
        return selectBookList(sql);
    }

    /**
     * 按照字符属性查询条件 获取图书对象列表重载方法
     * @param field
     * @param value
     * @return
     */
    public static List<Book> selectBookList(String field, String value){
        String sql = "select * from book";
        if((value != null & value.length() > 0) && field != null){
            sql = "select * from book where " + field + " like '%" + value + "%'";
        }
        return selectBookList(sql);
    }

    /**
     * 按照double类型查询条件 获取图书对象列表重载方法
     * @param field
     * @param value
     * @return
     */
    public static List<Book> selectBookList(String field, double value){
        String sql = "select * from book";
        if(field != null&& value != 0){
            sql = "select * from book where" + field + "between" + ((int)(value/ 10)*10)+ "and" + ((int)(value/10)*10 + 10);
        }
        return selectBookList(sql);
    }

    /**
     * 按照整型查询条件 获取图书列表重载方法
     * @param field
     * @param value
     * @return
     */
    public static List<Book> selectBookList(String field, int value){
        String sql = "select * from book";
        if(field != null&& value != 0){
            sql = "select * from book where" + field + "between" + ((int)(value/ 10)*10)+ "and" + ((int)(value/10)*10 + 10);
        }
        return selectBookList(sql);
    }

    /**
     * 添加新图书
     * @param book
     * @return
     */
    public static int insertBook(Book book){
        if(book != null){
            String id = book.getId();
            String name = book.getName();
            String type = book.getType();
            String author = book.getAuthor();
            String translator = book.getTranslator();
            String publisher = book.getPublisher();
            int stock = book.getStock();
            double price = book.getPrice();
            String sql = "insert into book values('"+ id + "','" + name + "','" + type + "','" + author + "','" + translator + "','" + publisher + "'," + stock + "," + price + ")";
            return BaseDao.executeUpdate(sql);
        }
        else{
            return -1;
        }
    }

    /**
     * 修改指定图书信息
     * @param book
     * @return
     */
    public static int updateBook(Book book){
        if(book != null){
            String id = book.getId();
            String name = book.getName();
            String type = book.getType();
            String author = book.getAuthor();
            String translator = book.getTranslator();
            String publisher = book.getPublisher();
            String publish_time = book.getPublish_time();
            int stock = book.getStock();
            double price = book.getPrice();
            String sql = "update book set name='" + name + "',type='" + type + "',author='" + author + "',translator='" + translator + "',publisher='" + publisher + "',publish_time='" + publish_time + "',stock=" + stock + ",price=" + price + " where id='" + id + "'";
            return BaseDao.executeUpdate(sql);
        }else{
            return -1;
        }
    }

    /**
     * 删除指定图书信息
     * @param id
     * @return
     */
    public static int deleteBook(String id){
        String sql = "delete from book where id='" + id + "'";
        return BaseDao.executeUpdate(sql);
    }

    /**
     * 清空所有图书信息
     */
    public void emptyBook(){
        String sql = "delete from book";
        BaseDao.executeUpdate(sql);
    }
}
